package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        StrategyContent strategyContent = strategyContentMapper.selectByPrimaryKey(id);
        strategyDetail.setStrategyContent(strategyContent);
        StrategyTag strategyTag = strategyTagService.get(id);
        return strategyDetail;
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {



        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategyDetail.getCatalog().getId());
        Destination dest = new Destination();
        dest.setId(strategyCatalog.getDestId());
        strategyDetail.setDest(dest);

        if(dest.getId() == 1L){
            strategyDetail.setIsabroad(StrategyDetail.ABROAD_NO);
        }

        //摘要 summary
        //方案1: 由运营人员自己编写
        //方案2: 截取文章内容前200个字
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        String content = strategyContent.getContent();
        if(content.length() > 200){
            strategyDetail.setSummary(content.substring(0, 200));
        }else {
            strategyDetail.setSummary(content);
        }

        String[] split = tags.split(",");
        if(strategyDetail.getId() == null){


            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

            // 保存标签
            for (String tagName : split) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(tagName);
                strategyTagService.saveOrUpdate(strategyTag);
                strategyDetailMapper.saveStrategyTagRelation(strategyTag.getId(), strategyDetail.getId());
            }

        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyDetailMapper.deleteStrategyTagRelation(strategyDetail.getId());
            for (String tagName : split) {
                StrategyTag strategyTag = strategyTagService.getByName(tagName);
                strategyDetailMapper.saveStrategyTagRelation(strategyTag.getId(), strategyDetail.getId());
            }
        }


    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long id) {
        return strategyDetailMapper.selectViewNumTop3(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyDetailMapper.selectContent(id);
    }

    @Override
    public void updateStatisVO(StrategyPersistenceVO pvo) {
        strategyDetailMapper.updateStatisVO(pvo);
    }


}
